<?php
/**
 * Created by PhpStorm.
 * User: wangliang
 * Date: 2018/12/14
 * Time: 11:59 AM
 */

namespace app\Model;
use App\Model\Base;
use PDO;
use Illuminate\Support\Facades\DB;

class OrderModel extends Base
{
    /** 获取订单列表
     * @return \Illuminate\Support\Collection
     */
    public function getOrderList(){
       $list =  DB::table('user_order')->get();
       return $list;
    }

    /**
     * 订单里面的产品
     * @return \Illuminate\Support\Collection
     */
    public function getOrderGoodsList($param){
        $map = [];
        if(!empty($param['goods_id'])){
            $map['o.goods_id'] = $param['goods_id'];
        }
        if(!empty($param['category'])){
            $map['gf.category_id'] = $param['category'];
        }

        $db =  DB::table('user_order_detailed as o')
            ->leftJoin('goods_model as gm','gm.goods_id','=', DB::raw('o.goods_id and gm.goods_model_id = o.model_id'))
            ->leftJoin('goods_master as gf','gf.goods_id','=','o.goods_id')
            ->leftJoin('user_order as uo','uo.order_id','=','o.order_id')
            ->select(DB::raw('o.*,gm.color,gm.size,gf.category_id,gf.goods_code,uo.create_time,uo.add'))
            ->where($map);

        if(!empty($param['star_date']) && !empty($param['end_date'])){
            $list = $db->whereBetween('uo.create_time',[$param['star_date'],$param['end_date']])->get();
        }else{
         $list = $db->get();
        }
        return $list;
    }

    /** 获取订单的详细数据
     * @param $order_id
     *
     * @return array
     */
    public function getUserOrderInfo($order_id){

        $orderInfo = DB::table('user_order')->where('order_id','=',$order_id)->first();
        $orderDetail = DB::table('user_order_detailed as od')
                        ->leftJoin('goods_master as gm','od.goods_id','=','gm.goods_id')
                        ->leftJoin('category_master as cm','gm.category_id','=','cm.category_id')
                        ->leftJoin('inout_log as io','io.goods_id','=',DB::raw("od.goods_id and io.order_id='$order_id' and io.model_id=od.model_id"))
                        ->select(DB::raw('od.*,gm.goods_code,cm.category_name,io.num'))
                        ->where('od.order_id','=',$order_id)
                        ->get();

        return array('orderInfo'=>get_object_vars($orderInfo),'orderDetail'=>$orderDetail);
    }

    /**
     * 销售统计查询
     * @param $param
     *
     * @return array|\Illuminate\Support\Collection
     */
    public function orderDataStatistics($param){
        $map = [];
        if(!empty($param['corp_id'])){
            $map['uo.corp_id'] = $param['corp_id'];
        }
        if(!empty($param['category'])){
            $map['uo.shop_id'] = $param['shop_id'];
        }
        if(!empty($param['no_money'])){
            $map['uo.state'] = 1;
        }


        $db =  DB::table('user_order_detailed as o')
            ->leftJoin('goods_model as gm','gm.goods_id', DB::raw('o.goods_id and gm.goods_model_id = o.model_id'))
            ->leftJoin('goods_master as gf','gf.goods_id','=','o.goods_id')
            ->leftJoin('user_order as uo','uo.order_id','=','o.order_id')
            ->leftJoin('inout_log as io','io.goods_id','=',DB::raw("o.goods_id and io.order_id=o.order_id and io.model_id=o.model_id"))
            ->select(DB::raw('o.*,gm.color,gm.size,gf.category_id,gf.goods_code,uo.create_time,uo.add,uo.corp_id,uo.shop_id,io.num'))
            ->where($map);

        if(!empty($param['star_date']) && !empty($param['end_date'])){
            $list = $db->whereBetween('uo.create_time',[$param['star_date'],$param['end_date']])->get();
        }else{
            $list = $db->get();
        }

        if($list != false){
            $data = [];
            foreach ($list as $value){
                $data[$value->corp_id][] = $value;
            }
            $list = $data;
        }

        return $list;
    }

    /**
     * 店铺的销售统计
     * @param $param
     *
     * @return array|\Illuminate\Support\Collection
     */
    public function shopDataStatistics($param){

        $db = DB::table('user_order')->select(DB::raw('*,sum(money) as total_money'))->groupBy(['shop_id']);

        if(!empty($param['star_date']) && !empty($param['end_date'])){
            $list = $db->whereBetween('create_time',[$param['star_date'],$param['end_date']])->get();
        }else{
            $list = $db->get();
        }

        if($list != false){
            $data = [];
            foreach ($list as $value){
                $data[$value->corp_id][] = $value;
            }
            $list = $data;
        }
        return $list;
    }

}
